//
//  LXSelectNumberView.m
//  TestDemo
//
//  Created by xiao.zhao@lachesis-mh.com on 16/2/23.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXSelectNumberView.h"

@interface LXSelectNumberView ()

@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UIButton *reduceBtn;

@end

@implementation LXSelectNumberView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

-(void)setup
{
    _maxSelect = 1000;//初始化
    
    self.increment = 1.0f;
    self.isDecimal = NO;
    
    _addBtn = [[UIButton alloc]init];
    [_addBtn setBackgroundImage:[UIImage imageNamed:@"icons_add_normal"] forState:UIControlStateNormal];
    [_addBtn setBackgroundImage:[UIImage imageNamed:@"icons_add_press"] forState:UIControlStateHighlighted];
    [_addBtn addTarget:self action:@selector(addNumber) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_addBtn];
    
    _reduceBtn = [[UIButton alloc]init];
    [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"icons_delate_normal"] forState:UIControlStateNormal];
    [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"icons_delete_press"] forState:UIControlStateHighlighted];
    [_reduceBtn addTarget:self action:@selector(reduceNumber) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_reduceBtn];
    
    [_reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self);
        make.right.equalTo(_addBtn.mas_left).offset([LXAutoLength lx_L:0.0f]);
    }];
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.width.equalTo(_reduceBtn.mas_width);
    }];
    
}

- (void)setMaxSelect:(CGFloat)maxSelect
{
    _maxSelect = maxSelect;
}

- (void)setMinSelect:(CGFloat)minSelect
{
    _minSelect = minSelect;
}

- (void)setUnit:(NSInteger)unit
{
    _unit = unit;
}

-(void)addNumber
{
    if (self.quantity < _maxSelect) {
        self.quantity += self.increment;
    } else {
        self.quantity = _maxSelect;
    }
}

-(void)reduceNumber
{
    if (self.quantity > _minSelect) {
        self.quantity -= self.increment;
    } else {
        self.quantity = _minSelect;
    }
}

-(void)setQuantity:(CGFloat)quantity
{
    _quantity = quantity;
//    NSInteger newUnit = _unit == 0 ? 1 : _unit;
//    if(_isDecimal)
//    {
//        _numberString = [NSString stringWithFormat:@"%.1f%@", self.quantity * newUnit, _unitName];
//    }
//    else
//    {
//        _numberString = [NSString stringWithFormat:@"%ld%@", (int)self.quantity * newUnit, _unitName];
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
